package modelLayer;
import java.util.ArrayList;


/**
 * This is a container to the class Loan
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoanContainer
{
    // instance variables - replace the example below with your own
    private ArrayList<Loan> loans;
    private static LoanContainer instance;

    /**
     * Constructor for objects of class LoanContainer
     */
    private LoanContainer()
    {
        loans = new ArrayList<Loan>();
    }
    
    /**
     * Method getInstance
     * 
     * singleton
     *
     * @return The return value
     */
    public static LoanContainer getInstance()
    {
        if(instance == null)
        {
            instance = new LoanContainer();
        }
        return instance;
    }

    /**
     * Method getLoans
     *
     * @return The return value
     */
    public ArrayList<Loan> getLoans()
    {
        return loans;
    }
    
    /**
     * Method addLoan
     * 
     * adds a loan l to the arraylist loans
     *
     * @param l A parameter
     */
    public void addLoan(Loan l)
    {
        loans.add(l);
    }
    
    /**
     * Method findLoan
     * 
     * This method will search for an is, and return the corresponding loan
     *
     * @param id A parameter
     * @return The return value
     */
    public Loan findLoan(String id)
    {
        Loan returnLoan = null;
        boolean found = false;
        int size = loans.size();
        for(int i = 0;  i < size && !found;i++)
        {
            if(loans.get(i).getId().equals(id))
            {
                returnLoan = loans.get(i);
                found = true;
            }
        }
        return returnLoan;
    }
    
    
    /**
     * Method updateLoan
     * 
     * searches for the loan's id, and updates the loan's information
     *
     * @param oldId A parameter
     * @param id A parameter
     * @param borrowDate A parameter
     * @param period A parameter
     * @param newId A parameter
     * @return The return value
     */
    public boolean updateLoan(String oldId, String borrowDate, String period, String newId)
    {
        boolean successful = false;
        Loan tempL = findLoan(oldId);
        tempL.setBorrowDate(borrowDate);
        tempL.setId(newId);
        tempL.setPeriod(period);
        if(tempL.getBorrowDate().equals(borrowDate) && tempL.getId().equals(newId) && tempL.getPeriod().equals(period))
        {
            successful = true;
        }
        return successful;
    }
    
    /**
     * Method getState
     *
     * @param id A parameter
     * @return The return value
     */
    public boolean getState(String id)
    {
        return findLoan(id).getState();
    }
}
